#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_NoticeOfScheduledCarryInItemsSchemaModule:1:0:0}'
    ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1 + 'NoticeOfScheduledCarryInItems'
    
    #Read from inn by XML order

    #NoticeOfScheduledCarryInItems  
    TransportationStartDate = inn.get({'BOTSID':rootelement,ns1+'TransportationStartDate':None})
    TransportationEndDate = inn.get({'BOTSID':rootelement,ns1+'TransportationEndDate':None})
    AmendmentTypeCode = inn.get({'BOTSID':rootelement,ns1+'AmendmentTypeCode':None})
    ReleaseBaseNumberID = inn.get({'BOTSID':rootelement,ns1+'ReleaseBaseNumberID':None})
    TotalGrossMassMeasure = inn.get({'BOTSID':rootelement,ns1+'TotalGrossMassMeasure':None})
    TotalGrossMassMeasure_measureUnitCode = inn.get({'BOTSID':rootelement,ns1+'TotalGrossMassMeasure__measureUnitCode':None})
    
    #NoticeOfScheduledCarryInItems/Document
    DocumentTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'TypeCode':None})
    DocumentIssueDateTime = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'IssueDateTime':None})
    
    #NoticeOfScheduledCarryInItems/CarriedInLocation
    CarriedInLocationCodeID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CarriedInLocation',ns2+'CodeID':None})
    
    #NoticeOfScheduledCarryInItems/PreviousCustomsDocument
    PreviousCustomsDocumentReferenceID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'PreviousCustomsDocument',ns2+'referenceID':None})
    
    #NoticeOfScheduledCarryInItems/ReleaseCustomsOffice
    ReleaseCustomsOfficeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'ReleaseCustomsOffice',ns2+'OfficeCode':None})
    ReleaseCustomsOfficeCodeID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'ReleaseCustomsOffice',ns2+'CodeID':None})
    
    #NoticeOfScheduledCarryInItems/CarriedInCustomsOffice
    CarriedInCustomsOfficeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CarriedInCustomsOffice',ns2+'OfficeCode':None})
    CarriedInCustomsOfficeCodeID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CarriedInCustomsOffice',ns2+'CodeID':None})
    
    #NoticeOfScheduledCarryInItems/GoodsPackaging
    GoodsPackagingQuantity = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'GoodsPackaging',ns2+'Quantity':None})
    GoodsPackagingTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'GoodsPackaging',ns2+'TypeCode':None})
    
    out.ta_info['botskey'] = 'CUSMAN-5LP-' + PreviousCustomsDocumentReferenceID.strip()
    
    #Write to out by EDIFACT order
    
    out.put({'BOTSID':'UNH','0062':out.ta_info['reference']})
    out.put({'BOTSID':'UNH','S009.0065':'CUSMAN'})
    out.put({'BOTSID':'UNH','S009.0052':'S'})
    out.put({'BOTSID':'UNH','S009.0054':'93A'})
    out.put({'BOTSID':'UNH','S009.0051':'KE'})
            
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':DocumentTypeCode})
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':PreviousCustomsDocumentReferenceID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.7365':AmendmentTypeCode,'C529.1131':'5CM','C529.3055':'KCS'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'137','C507.2380':utils.edifactYYYYMMDDHHNNSS(DocumentIssueDateTime),'C507.2379':'204'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'163','C507.2380':utils.edifactYYYYMMDD(TransportationStartDate),'C507.2379':'102'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'164','C507.2380':utils.edifactYYYYMMDD(TransportationEndDate),'C507.2379':'102'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'LOC','3227':'130','C517.3225':CarriedInLocationCodeID,'C517.1131':'129','C517.3055':'KCS'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'LOC','3227':'42','C517.3225':ReleaseCustomsOfficeCodeID,'C517.1131':'113','C517.3055':'KCS','C519.3223':ReleaseCustomsOfficeCode,'C519.1131':'5AB','C519.3055':'KCS'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'LOC','3227':'41','C517.3225':CarriedInCustomsOfficeCodeID,'C517.1131':'113','C517.3055':'KCS','C519.3223':CarriedInCustomsOfficeCode,'C519.1131':'5AB','C519.3055':'KCS'})

    # Loop A 0 ~ 99
    cnt = 0
    for dms in inn.getloop({'BOTSID':rootelement},{'BOTSID':ns1+'BondedGoods'}):
        #NoticeOfScheduledCarryInItems/BondedGoods
        BondedGoodsSequenceID = dms.get({'BOTSID':ns1+'BondedGoods',ns1+'SequenceID':None})
        BondedGoodsGoodsTypeCode = dms.get({'BOTSID':ns1+'BondedGoods',ns1+'GoodsTypeCode':None})
        BondedGoodsGoodsTariffClassificationCode = dms.get({'BOTSID':ns1+'BondedGoods',ns1+'TariffClassificationCode':None})
        BondedGoodsGoodsTariffDescription = dms.get({'BOTSID':ns1+'BondedGoods',ns1+'Description':None})
        
        lou = out.putloop({'BOTSID':'UNH'},{'BOTSID':'DMS'})
        lou.put({'BOTSID':'DMS','1004':BondedGoodsSequenceID})
        
        lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'8AA','C108.4440#1':BondedGoodsGoodsTariffClassificationCode})
        lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'8AB','C108.4440#1':BondedGoodsGoodsTariffDescription})
        
        if cnt < 1:
            cnt = cnt + 1
            # Exclude from 2nd
            lou.put({'BOTSID':'DMS'},{'BOTSID':'DOC','C002.1001':'705','C503.1004':ReleaseBaseNumberID})
            lou.put({'BOTSID':'DMS'},{'BOTSID':'DOC'},{'BOTSID':'MEA','6311':'CT','C174.6411':GoodsPackagingTypeCode,'C174.6314':GoodsPackagingQuantity})
            lou.put({'BOTSID':'DMS'},{'BOTSID':'DOC'},{'BOTSID':'MEA','6311':'WT','C174.6411':TotalGrossMassMeasure_measureUnitCode,'C174.6314':TotalGrossMassMeasure})
            # End Exclude from 2nd
                
        # Loop B 0 ~ 99
        for rff in dms.getloop({'BOTSID':ns1+'BondedGoods'},{'BOTSID':ns1+'BondedGrade'}):
            #NoticeOfScheduledCarryInItems/BondedGoods/BondedGrade
            BondedGoodsBondedGradeSequenceID = rff.get({'BOTSID':ns1+'BondedGrade',ns1+'SequenceID':None})
            BondedGoodsBondedGradeGrade = rff.get({'BOTSID':ns1+'BondedGrade',ns1+'Grade':None})
            BondedGoodsBondedGradeBasePricePriceAmount = rff.get({'BOTSID':ns1+'BondedGrade'},{'BOTSID':ns1+'BasePrice',ns2+'PriceAmount':None})
            BondedGoodsBondedGradeBasePricePriceAmount_amountCurrencyID = rff.get({'BOTSID':ns1+'BondedGrade'},{'BOTSID':ns1+'BasePrice',ns2+'BondedGoodsBondedGradeBasePricePriceAmount__amountCurrencyID':None})
            BondedGoodsBondedGradeBasePriceBaseQuantity = rff.get({'BOTSID':ns1+'BondedGrade'},{'BOTSID':ns1+'BasePrice',ns2+'BaseQuantity':None})
            BondedGoodsBondedGradeBasePriceBaseQuantity_unitCode = rff.get({'BOTSID':ns1+'BondedGrade'},{'BOTSID':ns1+'BasePrice',ns2+'BaseQuantity__unitCode':None})
            
            lou2 = lou.putloop({'BOTSID':'DMS'},{'BOTSID':'RFF'})
            lou2.put({'BOTSID':'RFF','C506.1153':'ABU','C506.1156':BondedGoodsBondedGradeSequenceID})
            lou2.put({'BOTSID':'RFF'},{'BOTSID':'FTX','4451':'AAA','C108.4440#1':BondedGoodsBondedGradeGrade})
            lou2.put({'BOTSID':'RFF'},{'BOTSID':'FTX','4451':'PAY','C108.4440#1':BondedGoodsBondedGradeBasePricePriceAmount})
            lou2.put({'BOTSID':'RFF'},{'BOTSID':'MEA','6311':'CT','C174.6411':BondedGoodsBondedGradeBasePriceBaseQuantity_unitCode,'C174.6314':BondedGoodsBondedGradeBasePriceBaseQuantity})
        # End Loop B
    # End Loop A
        
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0074':out.getcount()+1})
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0062':out.ta_info['reference']}) 
    
